<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <cpn :number1="num1" :number2="num2" @num1change="num1change" @num2change="num2change"></cpn>
    </div>

    <template id="cpn">
        <div>
            <h2>props: {{number1}}</h2>
            <h2>data: {{dnumber1}}</h2>
            <!-- <input type="text" v-model="dnumber1"> -->
            <input type="text" :value="dnumber1" @input="num1Input">
            <h2>props: {{number2}}</h2>
            <h2>data: {{dnumber2}}</h2>
            <!-- <input type="text" v-model="dnumber2"> -->
            <input type="text" :value="dnumber2" @input="num2Input">
        </div>
    </template>

    <script src="../js/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                num1: 1,
                num2: 0
            },
            methods:{
                num1change(value){
                    this.num1 = Number(value) //传入的是String类型,将其转换为数字类型
                },
                num2change(value){
                    this.num2 = Number(value)
                }
            },
            components: {
                cpn: {
                    template: '#cpn',
                    //props仅用于存储父组件传递给子组件的数据(仅供展示用),不要在子组件中直接进行更改,如进行双向绑定,编译会报错
                    props: {
                        number1: Number,
                        number2: Number
                    },
                    data(){
                        return {
                        //如果要修改props中的数据,则将其放入到data中
                            dnumber1: this.number1,
                            dnumber2: this.number2
                        }
                    },
                    methods:{
                        num1Input(event){
                            this.dnumber1 = event.target.value
                            this.$emit('num1change',this.dnumber1)
                        },
                        num2Input(event){
                            this.dnumber2 = event.target.value
                            this.$emit('num2change',this.dnumber2)
                        }
                    }
                }
            }
        })
    </script>
</body>
</html>